package com.shujia.kafka

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties

object Demo1KafkaProducer {
  def main(args: Array[String]): Unit = {

    /**
     * 1、创建生产者
     */
    val properties = new Properties()
    //kafka集群的列表
    properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092")
    //数据key和value的格式
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //创建生产者，相当于和kafka建立连接
    val producer = new KafkaProducer[String, String](properties)

    //构建一行，指定topic和数据
    val record = new ProducerRecord[String, String]("bigdata", "大数据")

    //发送数据到kafka中
    producer.send(record)

    //关闭连接
    producer.close()

  }
}
